package swc.smu.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import swc.smu.adapter.ExpandAdapter;
import swc.smu.adapter.MainPagerAdapter;
import swc.smu.bean.ExpandListVO;

public class MainActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    ViewPager mViewPager;
    ExpandableListView expListView;
    SwipeRefreshLayout swipeRefreshLayout;

    ExpandAdapter expandAdapter;
    MainPagerAdapter mPagerAdapter;

    List<ExpandListVO> dataList;

    static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what) {
                case 0:
                    MainActivity Activity = (MainActivity) msg.obj;
                    Activity.swipeRefreshLayout.setRefreshing(false);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        generateDataList();
        initView();
        viewSetting();
    }

    private void viewSetting() {
        swipeRefreshLayout.setColorSchemeResources(R.color.swiperefresh_color_0, R.color.swiperefresh_color_1, R.color.swiperefresh_color_2, R.color.swiperefresh_color_3);
        swipeRefreshLayout.setOnRefreshListener(this);

        expandAdapter = new ExpandAdapter(MainActivity.this, dataList);
        expListView.setAdapter(expandAdapter);
        expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//                Intent intent = new Intent(MainActivity.this, CombinaActivity.class);
//                startActivity(intent);
                Toast.makeText(MainActivity.this, "I'm Parent Click", Toast.LENGTH_LONG).show();
                return false;
            }
        });

        int[] i = {R.mipmap.p0, R.mipmap.p1,R.mipmap.p2, R.mipmap.p3};
        mPagerAdapter = new MainPagerAdapter(MainActivity.this, i);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(10 * i.length, true);

        //viewpager������ͻ
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_MOVE:
                        swipeRefreshLayout.setEnabled(false);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        swipeRefreshLayout.setEnabled(true);
                        break;
                }
                return false;
            }
        });
    }

    private void initView() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        expListView = (ExpandableListView) findViewById(R.id.expand_list);
        mViewPager = (ViewPager) findViewById(R.id.float_viewPager);
    }

    private void generateDataList() {
        dataList = new ArrayList<ExpandListVO>();
        List<String> list;
        ExpandListVO expandListVO;
        for(int i = 0; i < 30; i++) {
            expandListVO = new ExpandListVO();
            list = new ArrayList<String>();
            for(int j = 0; i > 2 && j < 5; j++) {
                list.add("content --> " + j);
            }
            expandListVO.setTitle("title --> " + i);
            expandListVO.setDataList(list);
            dataList.add(expandListVO);
        }

    }

    @Override
    public void onRefresh() {
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Message msg = handler.obtainMessage();
                msg.what = 0;
                msg.obj = MainActivity.this;
                handler.sendMessage(msg);
//                swipeRefreshLayout.setRefreshing(false);
            }
        };
        new Timer().schedule(timerTask, 2000);
    }
}
